<template>
  <div class="bridge-experiment">
    <!-- 实验说明部分 -->
    <div class="theory-card">
      <h2 class="card-title">实验原理</h2>
      <p class="card-text">
        自组电桥是一种用于测量电阻的精密测量方法。通过调节电桥的平衡条件，可以准确测量未知电阻的值。实验中使用了以下公式：
      </p>
      <div class="formula-box">
        <p class="formula">R₁ = (R₂' × R₄) / R₃'</p>
        <p class="formula-note">其中：</p>
        <ul class="formula-list">
          <li>R₁ 为待测电阻</li>
          <li>R₂' 为平衡时的比率臂电阻</li>
          <li>R₃' 为平衡时的比率臂电阻</li>
          <li>R₄ 为已知电阻</li>
        </ul>
      </div>
    </div>

    <!-- 数据记录部分 -->
    <div class="data-card">
      <div class="card-header">
        <h2 class="card-title">实验数据</h2>
        <div class="button-group">
          <button @click="loadSampleData" class="btn-blue">加载示例数据</button>
          <button @click="clearData" class="btn-red">清空数据</button>
        </div>
      </div>
      <div class="data-table">
        <table>
          <thead>
            <tr>
              <th>R₄/Ω</th>
              <th>R₅/Ω</th>
              <th>R₅'/Ω</th>
              <th>R₃'/Ω</th>
              <th>R₂'/Ω</th>
              <th>R₁/Ω</th>
            </tr>
          </thead>
          <tbody>
            <!-- 第一组数据 -->
            <tr>
              <td>
                <input
                  v-model.number="experimentData.group1.R4"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
              <td>
                <div class="input-group">
                  <input
                    v-for="(value, index) in experimentData.group1.R5"
                    :key="index"
                    v-model.number="experimentData.group1.R5[index]"
                    type="number"
                    class="input-field"
                    step="0.1"
                  />
                </div>
              </td>
              <td>
                <input
                  v-model.number="experimentData.group1.R5prime"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
              <td>
                <div class="input-group">
                  <input
                    v-for="(value, index) in experimentData.group1.R3prime"
                    :key="index"
                    v-model.number="experimentData.group1.R3prime[index]"
                    type="number"
                    class="input-field"
                    step="0.1"
                  />
                </div>
              </td>
              <td>
                <input
                  v-model.number="experimentData.group1.R2prime"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
              <td>
                <input
                  v-model.number="experimentData.group1.R1"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
            </tr>
            <!-- 第二组数据 -->
            <tr>
              <td>
                <input
                  v-model.number="experimentData.group2.R4"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
              <td>
                <div class="input-group">
                  <input
                    v-for="(value, index) in experimentData.group2.R5"
                    :key="index"
                    v-model.number="experimentData.group2.R5[index]"
                    type="number"
                    class="input-field"
                    step="0.1"
                  />
                </div>
              </td>
              <td>
                <input
                  v-model.number="experimentData.group2.R5prime"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
              <td>
                <div class="input-group">
                  <input
                    v-for="(value, index) in experimentData.group2.R3prime"
                    :key="index"
                    v-model.number="experimentData.group2.R3prime[index]"
                    type="number"
                    class="input-field"
                    step="0.1"
                  />
                </div>
              </td>
              <td>
                <input
                  v-model.number="experimentData.group2.R2prime"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
              <td>
                <input
                  v-model.number="experimentData.group2.R1"
                  type="number"
                  class="input-field"
                  step="0.1"
                />
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 数据处理部分 -->
    <div class="processing-card">
      <h2 class="card-title">数据处理</h2>

      <!-- 第一组数据分析 -->
      <div class="analysis-section">
        <h3 class="section-title">
          R₄ = {{ experimentData.group1.R4 }}Ω 测量结果分析
        </h3>
        <div class="analysis-grid">
          <div class="analysis-box">
            <h4 class="box-title">R₅ 测量数据统计</h4>
            <p>平均值：{{ calculateAverage(experimentData.group1.R5) }}Ω</p>
            <p>标准偏差：{{ calculateStdDev(experimentData.group1.R5) }}Ω</p>
          </div>
          <div class="analysis-box">
            <h4 class="box-title">R₃' 测量数据统计</h4>
            <p>
              平均值：{{ calculateAverage(experimentData.group1.R3prime) }}Ω
            </p>
            <p>
              标准偏差：{{ calculateStdDev(experimentData.group1.R3prime) }}Ω
            </p>
          </div>
        </div>
      </div>

      <!-- 第二组数据分析 -->
      <div class="analysis-section">
        <h3 class="section-title">
          R₄ = {{ experimentData.group2.R4 }}Ω 测量结果分析
        </h3>
        <div class="analysis-grid">
          <div class="analysis-box">
            <h4 class="box-title">R₅ 测量数据统计</h4>
            <p>平均值：{{ calculateAverage(experimentData.group2.R5) }}Ω</p>
            <p>标准偏差：{{ calculateStdDev(experimentData.group2.R5) }}Ω</p>
          </div>
          <div class="analysis-box">
            <h4 class="box-title">R₃' 测量数据统计</h4>
            <p>
              平均值：{{ calculateAverage(experimentData.group2.R3prime) }}Ω
            </p>
            <p>
              标准偏差：{{ calculateStdDev(experimentData.group2.R3prime) }}Ω
            </p>
          </div>
        </div>
      </div>
    </div>

    <!-- 误差分析部分 -->
    <div class="error-card">
      <h2 class="card-title">误差分析</h2>
      <div class="error-grid">
        <div class="error-box">
          <h3 class="box-title">
            R₄ = {{ experimentData.group1.R4 }}Ω 测量结果
          </h3>
          <p>理论值：{{ experimentData.group1.R1 }}Ω</p>
          <p>
            实验值：{{
              calculateR1(
                experimentData.group1.R4,
                experimentData.group1.R2prime,
                calculateAverage(experimentData.group1.R3prime)
              )
            }}Ω
          </p>
          <p>
            相对误差：{{
              calculateRelativeError(
                experimentData.group1.R1,
                calculateR1(
                  experimentData.group1.R4,
                  experimentData.group1.R2prime,
                  calculateAverage(experimentData.group1.R3prime)
                )
              )
            }}%
          </p>
        </div>
        <div class="error-box">
          <h3 class="box-title">
            R₄ = {{ experimentData.group2.R4 }}Ω 测量结果
          </h3>
          <p>理论值：{{ experimentData.group2.R1 }}Ω</p>
          <p>
            实验值：{{
              calculateR1(
                experimentData.group2.R4,
                experimentData.group2.R2prime,
                calculateAverage(experimentData.group2.R3prime)
              )
            }}Ω
          </p>
          <p>
            相对误差：{{
              calculateRelativeError(
                experimentData.group2.R1,
                calculateR1(
                  experimentData.group2.R4,
                  experimentData.group2.R2prime,
                  calculateAverage(experimentData.group2.R3prime)
                )
              )
            }}%
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";

// 定义数据结构接口
interface ExperimentGroup {
  R4: number;
  R5: number[];
  R5prime: number;
  R3prime: number[];
  R2prime: number;
  R1: number;
}

interface ExperimentData {
  group1: ExperimentGroup;
  group2: ExperimentGroup;
}

// 初始化实验数据
const experimentData = reactive<ExperimentData>({
  group1: {
    R4: 0,
    R5: [0, 0, 0, 0, 0],
    R5prime: 0,
    R3prime: [0, 0, 0, 0, 0],
    R2prime: 0,
    R1: 0,
  },
  group2: {
    R4: 0,
    R5: [0, 0, 0, 0, 0],
    R5prime: 0,
    R3prime: [0, 0, 0, 0, 0],
    R2prime: 0,
    R1: 0,
  },
});

// 加载示例数据
const loadSampleData = () => {
  experimentData.group1 = {
    R4: 510,
    R5: [465.0, 465.2, 466.8, 466.6, 467.2],
    R5prime: 466.2,
    R3prime: [548.4, 547.0, 547.0, 546.1, 546.7],
    R2prime: 547.0,
    R1: 505.0,
  };
  experimentData.group2 = {
    R4: 10000,
    R5: [9428.7, 9450.7, 9440.0, 9434.4, 9436.2],
    R5prime: 9428.0,
    R3prime: [11072.7, 11047.1, 11066.2, 11066.3, 11068.5],
    R2prime: 11064.2,
    R1: 10218.8,
  };
};

// 清空数据
const clearData = () => {
  experimentData.group1 = {
    R4: 0,
    R5: [0, 0, 0, 0, 0],
    R5prime: 0,
    R3prime: [0, 0, 0, 0, 0],
    R2prime: 0,
    R1: 0,
  };
  experimentData.group2 = {
    R4: 0,
    R5: [0, 0, 0, 0, 0],
    R5prime: 0,
    R3prime: [0, 0, 0, 0, 0],
    R2prime: 0,
    R1: 0,
  };
};

// 计算平均值
const calculateAverage = (numbers: number[]): number => {
  if (!numbers.length || numbers.some((n) => !n)) return 0;
  const sum = numbers.reduce((acc, curr) => acc + curr, 0);
  return Number((sum / numbers.length).toFixed(1));
};

// 计算标准偏差
const calculateStdDev = (numbers: number[]): number => {
  if (!numbers.length || numbers.some((n) => !n)) return 0;
  const avg = calculateAverage(numbers);
  const squareDiffs = numbers.map((value) => Math.pow(value - avg, 2));
  const avgSquareDiff = calculateAverage(squareDiffs);
  return Number(Math.sqrt(avgSquareDiff).toFixed(1));
};

// 计算R1
const calculateR1 = (R4: number, R2prime: number, R3prime: number): number => {
  if (!R4 || !R2prime || !R3prime) return 0;
  return Number(((R2prime * R4) / R3prime).toFixed(1));
};

// 计算相对误差
const calculateRelativeError = (
  theoretical: number,
  experimental: number
): number => {
  if (!theoretical || !experimental) return 0;
  return Number(
    ((Math.abs(experimental - theoretical) / theoretical) * 100).toFixed(2)
  );
};
</script>

<style scoped>
.bridge-experiment {
  padding: 20px;
}

/* 卡片基础样式 */
.theory-card,
.data-card,
.processing-card,
.error-card {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 24px;
  margin-bottom: 24px;
}

/* 卡片标题 */
.card-title {
  font-size: 20px;
  font-weight: 700;
  color: #111827;
  margin-bottom: 16px;
}

/* 卡片文本 */
.card-text {
  color: #4b5563;
  margin-bottom: 16px;
}

/* 公式框 */
.formula-box {
  background-color: #f9fafb;
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 16px;
}

.formula {
  text-align: center;
  font-family: monospace;
}

.formula-note {
  font-size: 14px;
  color: #6b7280;
  margin-top: 8px;
}

.formula-list {
  font-size: 14px;
  color: #6b7280;
  list-style-type: disc;
  padding-left: 20px;
}

/* 卡片头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

/* 按钮组 */
.button-group {
  display: flex;
  gap: 8px;
}

.btn-blue {
  background-color: #eff6ff;
  color: #2563eb;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  transition: background-color 0.2s;
}

.btn-blue:hover {
  background-color: #dbeafe;
}

.btn-red {
  background-color: #fef2f2;
  color: #dc2626;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  transition: background-color 0.2s;
}

.btn-red:hover {
  background-color: #fee2e2;
}

/* 数据表格 */
.data-table {
  margin-top: 20px;
  overflow-x: auto;
}

table {
  width: 100%;
  min-width: 600px;
  border-collapse: separate;
  border-spacing: 0;
}

th,
td {
  border: 1px solid #e5e7eb;
  padding: 12px;
  text-align: center;
}

th {
  background-color: #f9fafb;
  font-weight: 500;
  color: #4b5563;
}

td {
  vertical-align: top;
}

/* 输入框样式 */
.input-field {
  width: 100px;
  padding: 6px 8px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  text-align: center;
  transition: all 0.2s;
}

.input-field:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* 分析网格 */
.analysis-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  margin-top: 12px;
}

.analysis-box {
  background-color: #f9fafb;
  padding: 16px;
  border-radius: 8px;
}

.box-title {
  font-weight: 500;
  margin-bottom: 8px;
}

/* 误差分析 */
.error-grid {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.error-box {
  background-color: #f9fafb;
  padding: 16px;
  border-radius: 8px;
}

.section-title {
  font-weight: 600;
  color: #111827;
  margin-bottom: 12px;
}

/* 移除数字输入框的上下箭头 */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="number"] {
  -moz-appearance: textfield;
}

@media (max-width: 640px) {
  .analysis-grid {
    grid-template-columns: 1fr;
  }
}
</style>
